<script setup>
import topNav from "../../components/topNav.vue";
import { onMounted, reactive, ref, getCurrentInstance, computed } from "vue";
import { onLoad, onReachBottom } from "@dcloudio/uni-app";
import { appProductQuery } from "@/services/goods";

// import tabbarView from "../components/tabbar.vue";
const scrollTop = ref(0);
const code = ref("");

onLoad((option) => {
  code.value = option.code ? option.code : "";
  form.value.keyword = option.code ? option.code : "";
});

const saveFun = () => {
  // console.log("取消");
  // form.value.name = "";
  // loading.value = "";
  // getAppProductQuery(1);
};
const searchFun = (val) => {
  console.log("取2消");
  form.value.keyword = val;
  loading.value = "";
  console.log(form.value.name);
  getAppProductQuery(1);
};

onReachBottom(() => {
  console.log("触底了");
  getAppProductQuery();
});
onMounted(() => {
  getAppProductQuery();
});

//获取商品
const form = ref({
  categoryId: "",
  pageIndex: 1,
  pageSize: 20,
  barCode: "",
  code: "",
  name: "",
  skuCode: "",
  categoryId: "",
  available: "",
  keyword: "",
  hasOfferPrice: true,
});

const productData = ref([]);

const loading = ref("");
const getAppProductQuery = async (index) => {
  if (index == 1) {
    productData.value = [];
  }
  if (index) {
    form.value.pageIndex = index;
  }
  if (loading.value == "noMore") {
    return;
  }
  loading.value = "Loading";
  try {
    let res = await appProductQuery(form.value);
    if (res.code == 200) {
      productData.value = productData.value.concat(res.data.datas);
      console.log(productData.value);
      form.value.pageIndex = form.value.pageIndex + 1;

      if (res.data.datas.length < form.value.pageSize) {
        loading.value = "noMore";
      } else {
        loading.value = "More";
      }
    } else {
    }

    console.log(res);
  } catch (e) {}
};

const ishowcamera = ref(false);
//编辑商品
const goEdt = (item) => {
  uni.navigateTo({ url: "/pages/goods/goods_add/index?id=" + item.id });
};
const scanFun = () => {
  console.log("扫码");
  // #ifdef H5
  uni.navigateTo({ url: "/pages/code_sand/index?type=goods" });
  // #endif
  // #ifdef APP-PLUS
  // 允许从相机和相册扫码

  const systemInfo = uni.getSystemInfoSync();
  if (systemInfo.osName === "android") {
    if (uni.getStorageSync("isspermission")) {
    } else {
      ishowcamera.value = true;
    }

    plus.android.requestPermissions(["android.permission.CAMERA"], (e) => {
      if (e.deniedAlways.length > 0) {
        // 弹出提示框解释为何需要权限，引导用户打开设置页面开启

        ishowcamera.value = false;
      } else if (e.deniedPresent.length > 0) {
        //权限被临时拒绝
        // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限
        ishowcamera.value = false;
      } else if (e.deniedAlways.length > 0) {
        //权限被永久拒绝
        // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限
        ishowcamera.value = false;
      } else {
        plus.android.requestPermissions(
          ["android.permission.READ_EXTERNAL_STORAGE"],
          (e) => {
            if (e.deniedAlways.length > 0) {
              // 弹出提示框解释为何需要权限，引导用户打开设置页面开启

              ishowcamera.value = false;
            } else if (e.deniedPresent.length > 0) {
              //权限被临时拒绝
              // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限

              ishowcamera.value = false;
            } else if (e.deniedAlways.length > 0) {
              //权限被永久拒绝
              // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限

              ishowcamera.value = false;
            } else {
              ishowcamera.value = false;
              uni.setStorageSync("isspermission", true);

              uni.scanCode({
                scanType: ["barCode", "qrCode"],
                autoDecodeCharset: true,
                autoZoom: true,
                success: function (res) {
                  console.log("条码类型：" + res.scanType);
                  console.log("条码内容：" + res.result);
                  form.value.keyword = res.result;
                  code.value = res.result;
                  getAppProductQuery(1);
                },
              });
            }
          }
        );
      }
    });
    return;
  }
  uni.scanCode({
    scanType: ["barCode", "qrCode"],
    autoDecodeCharset: true,
    autoZoom: true,
    success: function (res) {
      console.log("条码类型：" + res.scanType);
      console.log("条码内容：" + res.result);
      form.value.keyword = res.result;
      code.value = res.result;
      getAppProductQuery(1);
    },
  });
  // #endif
  // #ifdef APP-PLUS

  // if (uni.getSystemInfoSync().platform === "android") {
  //   console.log("android");
  //   const myScanCode = uni.requireNativePlugin("My-ScanCode");

  //   const options = {
  //     scanType: ["CODE_128", "QR_CODE"],
  //     prompt: "提示：将条形码/二维码图片对准扫描框即可自动扫描",
  //     locked: true,
  //   };
  //   console.log("sdasdas", myScanCode);
  //   myScanCode.scanCode(options, (res) => {
  //     if (res.success === "true") {
  //       form.value.keyword = res.result;

  //       code.value = res.result;
  //       uni.showToast({
  //         title: res.result,
  //         icon: "none",
  //       });
  //       getAppProductQuery(1);
  //     } else {
  //       uni.showToast({
  //         title: "扫码失败",
  //         icon: "none",
  //       });
  //     }
  //   });
  // } else {
  //   uni.scanCode({
  //     autoZoom: true,
  //     success: function (res) {
  //       console.log("条码类型：" + res.scanType);
  //       console.log("条码内容：" + res.result);
  //       form.value.keyword = res.result;
  //       code.value = res.result;
  //       getAppProductQuery(1);
  //     },
  //   });
  // }
  // #endif
  // #ifndef APP-PLUS || H5
  // uni.scanCode({
  //   autoZoom: true,
  //   success: function (res) {
  //     console.log("条码类型：" + res.scanType);
  //     console.log("条码内容：" + res.result);
  //     form.value.keyword = res.result;
  //     code.value = res.result;
  //     getAppProductQuery(1);
  //   },
  // });
  // #endif
};
</script>

<template>
  <topNav
    title="商品搜索"
    @save="saveFun"
    rType="cancel"
    @search="searchFun"
    @scan="scanFun"
    :IsSearch="true"
    :code="code"
    :isShowCode="false"
  ></topNav>
  <view class="goods_form">
    <view
      class="scroll-Y-item"
      v-for="(item, index) in productData"
      :key="index"
      @click="goEdt(item)"
    >
      <view class="scroll-item-left">
        <image :src="item.image" mode="scaleToFill" />
      </view>
      <view class="scroll-item-right">
        <view class="goods-item-title"> {{ item.name }} </view>
        <view class="goods-item-spec">
          条形码: {{ item.barCode ? item.barCode : "--" }}
        </view>
        <view class="goods-item-price">
          ¥{{ item.salePrice ? item.salePrice : 0.0 }}
          {{ item.unit ? "/" + item.unit : "" }}
        </view>
      </view>
    </view>
  </view>
  <view class="noData" v-if="productData.length <= 0 && loading == 'noMore'">
    <view class="noData-img">
      <image src="@/static/nodate/datas.png" mode="scaleToFill" />
      <view>暂无商品 </view>
    </view>
  </view>
  <view class="loading-form" v-if="loading && productData.length > 0"
    ><van-loading size="24rpx" v-if="loading == 'Loading'"
      ><text class="loading-text">加载中...</text>
    </van-loading>
    <text class="loading-text" v-if="loading == 'More'">加载更多</text>
    <text class="loading-text" v-if="loading == 'noMore'">~没有数据了~</text>
  </view>

  <view class="show-camera" v-if="ishowcamera">
    <view class="show-camera-k">
      <view class="show-sm"> 设备权限使用说明 </view>
      <view class="show-des">
        为了给您带来更好的服务，需要获取您的相机权限与读取图片及文件权限，用于更换上传图片、扫一扫等功能，对于您授权的信息我们竭尽全力提供安全保护。
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>
